package part3;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class DatagramPacket2Packet {
	public DatagramPacket2Packet(){}
	public DatagramPacket2Packet(DatagramPacket packet) {
		dealWith(packet);
	}
	public Packet dealWith(DatagramPacket Hpacket) {
		Packet p = null;
		String data = new String(Hpacket.getData());
		String PacketData = data.substring(0,data.indexOf("\r\n"));
		String[] str = PacketData.split(" ");
		String[] str_1 = new String[str.length];
		for (int i = 0; i < str.length; i++) {
			str_1[i] = str[i].substring(str[i].indexOf(":") + 1,str[i].length());
		}
		
		p = new Packet(Short.parseShort(str_1[5]), Short.parseShort(str_1[6]),
				Integer.parseInt(str_1[7]), Integer.parseInt(str_1[8]),
				Boolean.parseBoolean(str_1[9]),
				Boolean.parseBoolean(str_1[10]),
				Boolean.parseBoolean(str_1[11]),
				Boolean.parseBoolean(str_1[12]),
				Boolean.parseBoolean(str_1[13]),
				Boolean.parseBoolean(str_1[14]), Byte.parseByte(str_1[15]),
				Byte.parseByte(str_1[16]), Short.parseShort(str_1[17]),
				Short.parseShort(str_1[18]), Short.parseShort(str_1[19]),Byte.parseByte(str_1[20]),
				str_1[21]);
		try {
			p.setDst(InetAddress.getByName(str_1[0].substring(1)));
			p.setSrc(InetAddress.getByName(str_1[1].substring(1)));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		p.setProtocol(Byte.parseByte(str_1[2]));
		p.setMzb(Byte.parseByte(str_1[3]));
		p.setTCPlength(Short.parseShort(str_1[4]));
		
		return p;
	}
}
